package com.zingrow.web.support.operate.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.common.utils.ExcelUtils;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.base.CodeMsg;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.support.operate.model.AppOptRecord;
import com.zingrow.web.support.operate.model.OptRecord;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.user.model.ActiveUser;
import com.zingrow.web.user.model.UserPermission;
import com.zingrow.web.user.service.IPermissionService;

@Controller
@RequestMapping(value = "/record/", produces = "application/json;charset=UTF-8")
public class OptRecordController extends BaseController {
	@Autowired
	IOptRecordService optRecordService;
	@Autowired
	private IPermissionService permissionService;

	/**
	 * 根据操作用户名和操作时间段查询
	 * 
	 * @param request
	 * @param response
	 * @param pageIndex
	 * @param pageSize
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByParams")
	public String queryByParams(HttpServletRequest request, Integer pageIndex, Integer pageSize, String userName,
			String startTime, String endTime,String channelname,String dongzname) {
		List<OptRecord> returnList = new ArrayList<OptRecord>();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				returnList);
		PageView pageView = null;

		try {
			// 设置分页
			if (pageIndex == null || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (pageSize == null || pageSize <= 0) {
				pageSize = 10;
			}
			pageView = new PageView(pageSize, pageIndex);
			// 字符转换
			if (userName != null || !"".equals(userName)) {
				userName = EncodingUtils.fixUrlParamEncoding(request, userName);
			}
			if (channelname != null || !"".equals(channelname)) {
				channelname = EncodingUtils.fixUrlParamEncoding(request, channelname);
			}
			returnList = optRecordService.queryByParams(pageView, userName, startTime, endTime,channelname,dongzname);

		} catch (Exception e) {
			log.error("根据参数查询用户异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			standardData.setReturnData(returnList);
		}
		standardData.setReturnData(returnList);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		standardData.setTotalPages(pageView.getPageCount());
		standardData.setTotalRecords(pageView.getRowCount());
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 根据操作用户名和操作时间段查询
	 * 
	 * @param request
	 * @param response
	 * @param pageIndex
	 * @param pageSize
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("appqueryByParams")
	public String appqueryByParams(HttpServletRequest request, Integer pageIndex, Integer pageSize, String userName,
			String startTime, String endTime, String userAccount, String optChannel) {
		List<AppOptRecord> returnList = new ArrayList<AppOptRecord>();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				returnList);
		PageView pageView = null;

		try {
			// 设置分页
			if (pageIndex == null || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (pageSize == null || pageSize <= 0) {
				pageSize = 10;
			}
			pageView = new PageView(pageSize, pageIndex);
			// 字符转换
			if (userName != null || !"".equals(userName)) {
				userName = EncodingUtils.fixUrlParamEncoding(request, userName);
			}
			if (userAccount != null || !"".equals(userAccount)) {
				userAccount = EncodingUtils.fixUrlParamEncoding(request, userAccount);
			}

			if (optChannel != null || !"".equals(optChannel)) {
				optChannel = EncodingUtils.fixUrlParamEncoding(request, optChannel);
			}

			returnList = optRecordService.appqueryByParams(pageView, userAccount, startTime, endTime, userName,
					optChannel);

		} catch (Exception e) {
			log.error("根据参数查询用户异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			standardData.setReturnData(returnList);
		}
		standardData.setReturnData(returnList);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		standardData.setTotalPages(pageView.getPageCount());
		standardData.setTotalRecords(pageView.getRowCount());
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	@ResponseBody
	@RequestMapping("exportExcelForRecord")
	public String exportExcelForModel(HttpServletRequest request, HttpServletResponse response,String userName,
			String startTime,String endTime,String channelname,String dongzname) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		List<OptRecord> list = new ArrayList<OptRecord>();
		try {
			List<String> titles = Arrays.asList("管理员", "操作模块","子模块", "操作行为", "操作内容","操作时间","IP","备注");
			List<String> paramsName = Arrays.asList("userAccount", "optChannel","optminChannel", "optType","optName","insertTime","ip","remarks");
			if (userName != null || !"".equals(userName)) {
				userName = EncodingUtils.fixUrlParamEncoding(request, userName);
			}
			if (channelname != null || !"".equals(channelname)) {
				channelname = EncodingUtils.fixUrlParamEncoding(request, channelname);
			}if (dongzname != null || !"".equals(dongzname)) {
				dongzname = EncodingUtils.fixUrlParamEncoding(request, dongzname);
			}
			list = optRecordService.queryByParams(null, userName, startTime, endTime,channelname,dongzname);
			OutputStream out = null;
			try {
				// 获取输出流
				out = response.getOutputStream();
				// 清空流
				response.reset();
				// 定义响应输出类型
				response.setContentType("application/msexcel");
				// 定义输出文件头
				response.setHeader("Content-disposition",
						"attachment; filename=" + new String("操作日志".getBytes("GBK"), "iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titles, paramsName, list, out);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				// 关闭流
				out.close();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// app日志导出
	@ResponseBody
	@RequestMapping("exportExcelAppRecord")
	public String exportExcelAppRecord(HttpServletRequest request, HttpServletResponse response, String userName,
			String startTime, String endTime, String userAccount, String optChannel) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		// 参数字符转换
		if (userName != null || !"".equals(userName)) {
			userName = EncodingUtils.fixUrlParamEncoding(request, userName);
		}
		if (userAccount != null || !"".equals(userAccount)) {
			userAccount = EncodingUtils.fixUrlParamEncoding(request, userAccount);
		}

		if (optChannel != null || !"".equals(optChannel)) {
			optChannel = EncodingUtils.fixUrlParamEncoding(request, optChannel);
		}
		List<AppOptRecord> list = new ArrayList<AppOptRecord>();
		try {
			List<String> titles = Arrays.asList("用户id", "账号", "用户名称", "手机型号", "手机版本", "操作模块", "子模块", "操作内容", "操作时间",
					"备注");
			List<String> paramsName = Arrays.asList("userId", "userAccount", "userName", "phoneType", "phoneSystem",
					"optChannel", "optMinChannel", "optDeed", "insertTime", "notes");
			list = optRecordService.appqueryByParams(null, userAccount, startTime, endTime,userName , optChannel);

			OutputStream out = null;
			try {
				// 获取输出流
				out = response.getOutputStream();
				// 清空流
				response.reset();
				// 定义响应输出类型
				response.setContentType("application/msexcel");
				// 定义输出文件头
				response.setHeader("Content-disposition",
						"attachment; filename=" + new String("App操作日志".getBytes("GBK"), "iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titles, paramsName, list, out);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				// 关闭流
				out.close();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

}
